package org.multimedia.jolitorask.client;

import java.rmi.RemoteException;
import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;

import org.multimedia.jolitorask.client.exception.LoginException;
import org.multimedia.jolitorask.client.exception.MailException;

@SuppressWarnings("unchecked")
public abstract class ClientEntity implements EntityBean{

	private static final long serialVersionUID = 1L;


	 /* Contexte de l'EJB */
	 @SuppressWarnings("unused")
	 private EntityContext context;
	 
	 /** Methodes Home */
	 public abstract Integer getId();
	 public abstract String getLogin();
	 public abstract String getEmail();
	 public abstract String getUrl();
	 public abstract String getPass();
	 public abstract void setId(Integer i);
	 public abstract void setLogin(String l);
	 public abstract void setEmail(String e);
	 public abstract void setUrl(String u);
	 public abstract void setPass(String p);

	 public abstract Collection getUtilisateurs();
	 public abstract void setUtilisateurs(Collection utilisateurs);
	 	 
	 
	 @SuppressWarnings("unchecked")
	public Integer ejbCreate(Integer i, String l, String p, String e, String u)throws CreateException {
		 
		 Collection<ClientLocal> c = null; 
			 
		 try {
			 
			c= ((ClientLocalHome)context.getEJBLocalHome()).findByLogin(l);
			
		} catch (FinderException e1) {
			
			c = null;
		}
		 
		 
		 
		if( c != null )
			throw new LoginException();
		
		try {
			 
			c= ((ClientLocalHome)context.getEJBLocalHome()).findByMail(l);
			
		} catch (FinderException e1) {
			
			c = null;
		}
		 
		 
		 
		if( c != null )
			throw new MailException();
		 
		 setLogin(l);
		 setPass(p);
		 setEmail(e);
		 setUrl(u);
		 setId(i);
		 
		 return i;
	 }
	 
	 public void ejbPostCreate(Integer i, String n, String p, String e, String u) throws EJBException{
	 }

	 
	public void setEntityContext(EntityContext c) throws EJBException, RemoteException {
		context = c;
	}

	public void unsetEntityContext() throws EJBException, RemoteException {
		context = null;
	}
	
	
	public void ejbActivate() throws EJBException, RemoteException {}
	public void ejbLoad() throws EJBException, RemoteException {}
	public void ejbPassivate() throws EJBException, RemoteException {}
	public void ejbRemove() throws RemoveException, EJBException, RemoteException {}
	public void ejbStore() throws EJBException, RemoteException {}

}


